﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Blog.Types.Repository;

namespace Blog.Core
{
    public class MessageCollection : BaseObjectCollection<Message>
    {
        public MessageCollection(Theme theme)
            :this(theme == null ? 0 : theme.ID)
        {
            Theme = theme;
        }

        public MessageCollection(int themeId)
        {
            ThemeId = themeId;
        }

        public Theme Theme
        {
            get;
            private set;
        }

        public int ThemeId
        {
            get;
            private set;
        }

        protected override void LoadExecute()
        {
            if (ThemeId == 0)
                return;

            foreach (IMessage mess in Factory.Instance.Repository.GetMessages(ThemeId))
                Add(new Message(Theme, ThemeId, mess));
        }

        protected override Message GenerateNewObject()
        {
            return new Message(Theme);
        }

        public IEnumerable<Message> GetLevel(int treeId, int parentMessageId)
        {
            if (treeId == 0)
            {
                return from mes in this
                       where mes.ParentId == 0
                       select mes;                       
            }
            else 
            {
                return from mes in this
                       where mes.ParentId == parentMessageId
                       select mes;
            }
        }
    }
}
